package com.store.app.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.store.app.R;
import com.store.app.bean.OrderBuyBean;
import com.store.app.utils.MoneyUtil;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

public class OrderBuyAdapter extends BaseAdapter {
    private List<OrderBuyBean> goodsList;
    private Context context;
    private OnClickOrder onClickOrder;
    private LinearLayoutManager linearLayoutManager;
    private List<String> urlList;

    public interface OnClickOrder {
        void cancleClick(OrderBuyBean bean);//取消

        void confirmClick(OrderBuyBean bean);//确认


        void queryLogistics(OrderBuyBean bean);//查看物流

        void goPay(OrderBuyBean bean);//去支付

        void goDetails(OrderBuyBean bean);//跳到详情页面

    }

    public OrderBuyAdapter(List<OrderBuyBean> goodsList, Context context, OnClickOrder onClickOrder) {
        this.goodsList = goodsList;
        this.context = context;
        this.onClickOrder = onClickOrder;

        linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        urlList = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return goodsList.size();
    }

    @Override
    public Object getItem(int position) {
        return goodsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final OrderBuyBean orderBean = goodsList.get(position);

        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_order_wyp, null);
            holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            holder.tv_order_state = (TextView) convertView.findViewById(R.id.tv_order_state);
            holder.tv_order_no = (TextView) convertView.findViewById(R.id.tv_order_no);
            holder.tv_order_create_time = (TextView) convertView.findViewById(R.id.tv_order_create_time);
            holder.tv_count = (TextView) convertView.findViewById(R.id.tv_count);
            holder.tv_money = (TextView) convertView.findViewById(R.id.tv_money);
            holder.tv_cancel = (TextView) convertView.findViewById(R.id.tv_cancel);
            holder.tv_logistics = (TextView) convertView.findViewById(R.id.tv_logistics);
            holder.tv_del_fee = (TextView) convertView.findViewById(R.id.tv_del_fee);
            holder.tv_ok = (TextView) convertView.findViewById(R.id.tv_ok);
            holder.tv_pay = (TextView) convertView.findViewById(R.id.tv_pay);
            holder.iv_one = (ImageView) convertView.findViewById(R.id.iv_one);
            holder.iv_two = (ImageView) convertView.findViewById(R.id.iv_two);
            holder.iv_three = (ImageView) convertView.findViewById(R.id.iv_three);
            holder.iv_four = (ImageView) convertView.findViewById(R.id.iv_four);
            holder.iv_five = (ImageView) convertView.findViewById(R.id.iv_five);
            holder.rl_show_price = (RelativeLayout) convertView.findViewById(R.id.rl_show_price);
            holder.rl_bottom_btn = (RelativeLayout) convertView.findViewById(R.id.rl_bottom_btn);
            holder.llContain = (LinearLayout) convertView.findViewById(R.id.ll_contain);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if(MoneyUtil.moneyBigThan(orderBean.getDelivery_fee(),"0")){
            holder.tv_del_fee.setVisibility(View.VISIBLE);
            holder.tv_del_fee.setText("(配送费"+orderBean.getDelivery_fee()+")");
        }else{
            holder.tv_del_fee.setVisibility(View.GONE);
        }

        //正常订单
        holder.tv_title.setText(orderBean.getSupplier());
        holder.tv_order_no.setText(orderBean.getOrder_no());
        holder.tv_order_create_time.setText(orderBean.getOrder_date());
        holder.tv_count.setText(orderBean.getItem_num() + "");
        holder.tv_money.setText(orderBean.getTotal_fee());
        urlList.clear();
        String jsonPicArray = orderBean.getGoods_pic_list();
        try {
            JSONArray jsonArray = new JSONArray(jsonPicArray);
            for (int i = 0; i < jsonArray.length(); i++) {
                String url = orderBean.getMap().get(jsonArray.getJSONObject(i).getString("path_id"));
                urlList.add(url);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (urlList.size() == 0) {
            holder.llContain.setVisibility(View.GONE);
        } else {
            Log.v("zyl","下标为："+position+"时候的图片集合大小:"+urlList.size());
            holder.llContain.setVisibility(View.VISIBLE);
            if (urlList.size() == 1) {
                holder.iv_one.setVisibility(View.VISIBLE);
                holder.iv_two.setVisibility(View.GONE);
                holder.iv_three.setVisibility(View.GONE);
                holder.iv_four.setVisibility(View.GONE);
                holder.iv_five.setVisibility(View.GONE);
                ImageLoader.getInstance().displayImage(urlList.get(0), holder.iv_one, options);
            } else if (urlList.size() == 2) {
                holder.iv_one.setVisibility(View.VISIBLE);
                holder.iv_two.setVisibility(View.VISIBLE);
                holder.iv_three.setVisibility(View.GONE);
                holder.iv_four.setVisibility(View.GONE);
                holder.iv_five.setVisibility(View.GONE);
                ImageLoader.getInstance().displayImage(urlList.get(0), holder.iv_one, options);
                ImageLoader.getInstance().displayImage(urlList.get(1), holder.iv_two, options);
            } else if (urlList.size() == 3) {
                holder.iv_one.setVisibility(View.VISIBLE);
                holder.iv_two.setVisibility(View.VISIBLE);
                holder.iv_three.setVisibility(View.VISIBLE);
                holder.iv_four.setVisibility(View.GONE);
                holder.iv_five.setVisibility(View.GONE);
                ImageLoader.getInstance().displayImage(urlList.get(0), holder.iv_one, options);
                ImageLoader.getInstance().displayImage(urlList.get(1), holder.iv_two, options);
                ImageLoader.getInstance().displayImage(urlList.get(2), holder.iv_three, options);
            } else if (urlList.size() == 4) {
                holder.iv_one.setVisibility(View.VISIBLE);
                holder.iv_two.setVisibility(View.VISIBLE);
                holder.iv_three.setVisibility(View.VISIBLE);
                holder.iv_four.setVisibility(View.VISIBLE);
                holder.iv_five.setVisibility(View.GONE);
                ImageLoader.getInstance().displayImage(urlList.get(0), holder.iv_one, options);
                ImageLoader.getInstance().displayImage(urlList.get(1), holder.iv_two, options);
                ImageLoader.getInstance().displayImage(urlList.get(2), holder.iv_three, options);
                ImageLoader.getInstance().displayImage(urlList.get(3), holder.iv_four, options);
            } else if (urlList.size() >= 5) {
                holder.iv_one.setVisibility(View.VISIBLE);
                holder.iv_two.setVisibility(View.VISIBLE);
                holder.iv_three.setVisibility(View.VISIBLE);
                holder.iv_four.setVisibility(View.VISIBLE);
                holder.iv_five.setVisibility(View.VISIBLE);
                ImageLoader.getInstance().displayImage(urlList.get(0), holder.iv_one, options);
                ImageLoader.getInstance().displayImage(urlList.get(1), holder.iv_two, options);
                ImageLoader.getInstance().displayImage(urlList.get(2), holder.iv_three, options);
                ImageLoader.getInstance().displayImage(urlList.get(3), holder.iv_four, options);
                ImageLoader.getInstance().displayImage(urlList.get(4), holder.iv_five, options);
            }
        }

        holder.rl_bottom_btn.setVisibility(View.VISIBLE);
        String status = orderBean.getStatus();
        holder.rl_show_price.setVisibility(View.VISIBLE);
        if (!TextUtils.isEmpty(status)) {
            if (status.equals("processing")) {
                holder.tv_order_state.setText("处理中");
                holder.tv_cancel.setVisibility(View.VISIBLE);
                holder.tv_logistics.setVisibility(View.GONE);
                holder.tv_ok.setVisibility(View.GONE);
                holder.tv_pay.setVisibility(View.GONE);
            } else if (status.equals("delivered")) {
                holder.tv_order_state.setText("已发货");
                holder.tv_cancel.setVisibility(View.GONE);
                holder.tv_logistics.setVisibility(View.VISIBLE);
                holder.tv_ok.setVisibility(View.VISIBLE);
                holder.tv_pay.setVisibility(View.GONE);
            } else if (status.equals("payed")) {
                holder.rl_bottom_btn.setVisibility(View.GONE);
                holder.tv_order_state.setText("已付款");
                holder.tv_cancel.setVisibility(View.GONE);
                holder.tv_logistics.setVisibility(View.GONE);
                holder.tv_ok.setVisibility(View.GONE);
                holder.tv_pay.setVisibility(View.GONE);
            } else if (status.equals("received")) {
                holder.tv_order_state.setText("已收货");
                holder.tv_cancel.setVisibility(View.GONE);
                holder.tv_logistics.setVisibility(View.VISIBLE);
                holder.tv_ok.setVisibility(View.GONE);
                holder.tv_pay.setVisibility(View.GONE);
            } else if(status.equals("activing")){
                holder.rl_show_price.setVisibility(View.GONE);
                holder.tv_order_state.setText("预售中");
                holder.tv_cancel.setVisibility(View.VISIBLE);
                holder.tv_logistics.setVisibility(View.GONE);
                holder.tv_ok.setVisibility(View.GONE);
                holder.tv_pay.setVisibility(View.GONE);
            }else if (status.equals("cancelled")) {
                holder.rl_bottom_btn.setVisibility(View.GONE);
                holder.tv_order_state.setText("已取消");
                holder.tv_cancel.setVisibility(View.GONE);
                holder.tv_logistics.setVisibility(View.GONE);
                holder.tv_ok.setVisibility(View.GONE);
                holder.tv_pay.setVisibility(View.GONE);
            } else if (status.equals("non_paid")) {
                holder.tv_order_state.setText("待支付");
                holder.tv_cancel.setVisibility(View.VISIBLE);
                holder.tv_logistics.setVisibility(View.GONE);
                holder.tv_ok.setVisibility(View.GONE);
                holder.tv_pay.setVisibility(View.VISIBLE);
            } else {
                holder.rl_bottom_btn.setVisibility(View.GONE);
                holder.tv_order_state.setText("未知");
                holder.tv_cancel.setVisibility(View.GONE);
                holder.tv_logistics.setVisibility(View.GONE);
                holder.tv_ok.setVisibility(View.GONE);
                holder.tv_pay.setVisibility(View.GONE);
            }
        }

        holder.tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickOrder.cancleClick(orderBean);
            }
        });
        holder.tv_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickOrder.confirmClick(orderBean);
            }
        });
        holder.tv_logistics.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickOrder.queryLogistics(orderBean);
            }
        });
        holder.llContain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickOrder.goDetails(orderBean);
            }
        });
        holder.tv_pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickOrder.goPay(orderBean);
            }
        });
        return convertView;
    }


    public  class ViewHolder {
        public TextView tv_title;               //标题
        public TextView tv_order_state;         //订单状态
        public RecyclerView recycleview;
        public TextView tv_order_no;            //订单编号
        public LinearLayout ll_order_no;
        public TextView tv_order_create_time;   //订单创建时间
        public LinearLayout ll_create_time;
        public TextView tv_count;               //商品数量
        public TextView tv_money;               //合计
        public TextView tv_logistics;     //查看物流
        public TextView tv_cancel;        //取消
        public TextView tv_ok;         //确认收货
        public TextView tv_pay;                  //去支付
        public TextView tv_del_fee;               //邮费
        public ImageView iv_one;
        public ImageView iv_two;
        public ImageView iv_three;
        public ImageView iv_four;
        public ImageView iv_five;
        public LinearLayout llContain;
        public RelativeLayout rl_show_price;
        public RelativeLayout rl_bottom_btn;
    }

    DisplayImageOptions options = new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .showImageOnLoading(R.drawable.default_picture_s)
            .showImageOnFail(R.drawable.default_picture_s)
            .showStubImage(R.drawable.default_picture_s)
            .imageScaleType(ImageScaleType.EXACTLY)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();

}
